﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using Model;
using MyBind;

public partial class Communicate_TopicView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            if(Request["topId"]!=null)
            {
                string topId = Request["topId"].ToString();
                Session["topId"] = topId;
            }
            bind();
        }
    }

    public void bind()
    {
        if (Session["topId"] != null)
        {
            string topId = Session["topId"].ToString();
            DAL.bbsTopic dalTop = new DAL.bbsTopic();
            DataTable dtTop = dalTop.GetDetailedList("t.id=" + topId).Tables[0];
            if(dtTop.Rows.Count>0){
            this.lblFloor.Text = "楼主";
            lblUser.Text = dtTop.Rows[0]["name"].ToString();
            lblTitle.Text = dtTop.Rows[0]["title"].ToString();
            lblSendT.Text = dtTop.Rows[0]["sendTime"].ToString();
            lblCC.Text = dtTop.Rows[0]["clickCount"].ToString();
            lblRC.Text = dtTop.Rows[0]["replyCount"].ToString();
            lblContent.Text += dtTop.Rows[0]["content"].ToString();
            }

            DAL.bbsReply dalRep = new DAL.bbsReply();
            DataTable dtRep = dalRep.GetDetailedList("r.tpcId=" + topId).Tables[0];
            GridView2.DataSource = dtRep.DefaultView;
            GridView2.DataBind();

            this.lblsum.Text = "共有" + dtRep.Rows.Count+"个回复";
            for (int i = 0; i < dtRep.Rows.Count;i++ )
            {
                ((Label)GridView2.Rows[i].FindControl("lblFloor")).Text = (i+1)+"楼";
                ((Label)GridView2.Rows[i].FindControl("lblTitle")).Text = dtRep.Rows[i]["title"].ToString();
                ((Label)GridView2.Rows[i].FindControl("lblUser")).Text = dtRep.Rows[i]["name"].ToString();
                ((Label)GridView2.Rows[i].FindControl("lblContent")).Text += dtRep.Rows[i]["content"].ToString();
                ((Label)GridView2.Rows[i].FindControl("lblSendT")).Text = dtRep.Rows[i]["replyTime"].ToString();
            }
        }
    }


    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("BBSListView.aspx");
    }

    // 回复
    protected void Submit1_ServerClick(object sender, EventArgs e)
    {
        if (Session["topId"] != null && Session["UserName"]!=null)
        {
             if (this.txTitle.Value.Trim() == "" || this.HtmlEditor1_HtmlEditorContent.Value.Trim() == "")
            {

                this.lblErrorMsg.Text = "请填写完整信息!";
            }
            else
            {
                DAL.bbsReply dalRep = new DAL.bbsReply();
                Model.bbsReply mdlRep = new Model.bbsReply();

                mdlRep.tpcId = int.Parse(Session["topId"].ToString());
                //用户信息
                string UserName = Session["UserName"].ToString();
                Sqlselete s=new Sqlselete();
                mdlRep.userId= int.Parse(s.SelectEmpByUserName(UserName));

                mdlRep.title = txTitle.Value.Trim();
                mdlRep.replyTime = DateTime.Now;
                mdlRep.content = this.HtmlEditor1_HtmlEditorContent.Value.Trim();

                if (dalRep.Add(mdlRep)>1)
                {
                    WriteScript.Mesg(Response, "保存成功");
                }
                this.bind();
             }
        }
    }
}
